const IV = 'houguang';
const KEY = 'houguang#sina.cn';
App({
  globalData: {
    // javaApi: "https://www.anshangba.com/print",
    javaApi: "http://192.168.1.16:8080",
    printer: undefined,
    bindphone: undefined,
    isLogin: false,
    isAdmin: false,
    idCardArgee: false,
    agreePrivacy: false,
    appId: "wxa834ce0e0c020054"
  },
  onLaunch: function () {
    wx.getSystemInfo({
      success: (result) => {
        wx.setStorageSync('windowWidth', result.windowWidth);
        wx.setStorageSync('windowHeight', result.windowHeight);
      },
    })
    // 查看是否已登录
    let value = wx.getStorageSync("token")
    if (value) {
      this.globalData.isLogin = true
      let bindphone = wx.getStorageSync("bindphone")
      if (bindphone) {
        this.globalData.bindphone = bindphone
      }
    }
    // 查看是否已选择打印机
    let printer = wx.getStorageSync("printer")
    console.log("打印机", printer)
    if (printer) {
      this.globalData.printer = printer
    }
  },
  encrypt(str) {
    var key = CryptoJS.enc.Utf8.parse(KEY);
    var iv = CryptoJS.enc.Utf8.parse(IV);
    var encrypted = CryptoJS.AES.encrypt(str, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  },
  decrypt(str) {
    var key = CryptoJS.enc.Utf8.parse(KEY);
    var iv = CryptoJS.enc.Utf8.parse(IV);
    var decrypted = CryptoJS.AES.decrypt(str, key, {
      iv: iv,
      padding: CryptoJS.pad.Pkcs7
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
  },
  //上传文件
  upload(file, printer_id) {
    let page_type = wx.getStorageSync('page_type');
    return new Promise((resolve, reject) => {
      let path = file.path;
      // delete file["path"];
      let formData = Object.assign({
        page_type: page_type || 1,
        printerId: printer_id,
      }, file);
      wx.showLoading({
        title: '正在上传...',
        mask: true
      })
      wx.uploadFile({
        filePath: path,
        name: "file",
        formData: formData,
        timeout: 300000,
        header: { "Authorization": "Bearer " + wx.getStorageSync("token"), "AppId": this.globalData.appId },
        url: `${this.globalData.javaApi}/api/wx/print/file/upload`,
        success: res => {
          wx.hideLoading();
          console.log("上传成功", res);
          res = JSON.parse(res.data);
          if (res.code === 200) {
            resolve(res);
          } else if (res.code == 401) {
            wx.removeStorageSync('token');
            this.globalData.isLogin = false
            this.globalData.bindphone = undefined;
            wx.removeStorageSync('bindphone')
            wx.reLaunch({
              url: '/pages/wode/index',
            })
          }
          else {
            console.log("err", res)
            reject(res);
          }
        },
        fail: err => {
          wx.hideLoading();
          console.log("上传错误", err);
          reject(JSON.stringify(err))
        }
      })
    })
  },
  //数组删除指定元素
  remove: function (array, val) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] == val) {
        array.splice(i, 1);
        return array;
      }
    }
    return -1;
  },
})